package com.qingkai.onlineshop.presentation.dashboard

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.qingkai.onlineshop.R
import com.qingkai.onlineshop.data.entity.BannerEntity

/**
 *作者：created by 89270 on 2025/6/9 11:03
 *描述：
 */
@Composable
fun BannerList(modifier: Modifier, banners: List<BannerEntity>) {
    val pager = rememberPagerState { banners.size }
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        HorizontalPager(state = pager) { page ->
            AsyncImage(
                model = banners[page].url,
                contentDescription = "banner-${page}",
                contentScale = ContentScale.FillWidth,
                modifier = Modifier.fillMaxWidth(),
                error = painterResource(android.R.drawable.ic_menu_report_image)
            )
        }
        Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            repeat(banners.size) {
                var isCurrent = it == pager.currentPage
                Box(
                    modifier = Modifier
                        .width(if (isCurrent) 16.dp else 8.dp)
                        .height(8.dp)
                        .clip(CircleShape)
                        .background(
                            if (isCurrent) MaterialTheme.colorScheme.primary
                            else Color.LightGray
                        )
                )
            }
        }
    }

}

@Preview
@Composable
private fun BannerListPreview() {
    BannerList(banners = testBanners, modifier = Modifier.fillMaxWidth())
}

internal val testBanners = listOf(
    BannerEntity(R.drawable.banner_1, ""), BannerEntity(R.drawable.banner_2, "")
)